package com.mall.order.consumer;

import com.mall.order.OrderCancelService;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CancelOrderRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author
 * @date 2020/12/26 11:52
 */
@Component
@Slf4j
public class OrderDelayConsumer {

    private DefaultMQPushConsumer consumer;
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderCancelService orderCancelService;

    @PostConstruct
    public void init(){
        consumer = new DefaultMQPushConsumer("order_consumer_group");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        try {
            consumer.subscribe("order_delay","*");
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                try {
                    log.info("接收到消息");
                    MessageExt messageExt = list.get(0);
                    byte[] body = messageExt.getBody();
                    String orderId = new String(body);
                    Order order = orderMapper.selectByPrimaryKey(orderId);
                    //如果订单已经支付 则返回消费成功
                    Integer status = order.getStatus();
                    if (status == OrderConstants.ORDER_STATUS_PAYED) {
                        log.info("已支付 消费成功");
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                    if (status == OrderConstants.ORDER_STATUS_INIT) {
                        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
                        cancelOrderRequest.setOrderId(orderId);
                        orderCancelService.cancelOrder(cancelOrderRequest);
                        log.info("未支付 取消订单 消费成功");
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                } catch (Exception e) {
                    e.printStackTrace();
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
            }
        });

        try {
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        log.info("OrderDelayConsumer is start");
    }
}
